//
//  RHDeviceTopoBatteryItemView.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/24.
//  top图并机状态下的底部设备数据

#import "RHDeviceTopoBatteryItemView.h"
#import "KWTTopTipView.h"

@interface ItemPorgressView()


@end
@implementation ItemPorgressView

-(void)setProgress:(CGFloat)progress
{
    _progress = progress;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = self.width;
    [path moveToPoint:CGPointMake(self.width/2, self.height)];
    [path addLineToPoint:CGPointMake(self.width/2, self.height*(1-self.progress))];
    [kCOLOR_HEX(@"#5AD8A6") setStroke];
    [path stroke];
}

@end






@interface RHDeviceTopoBatteryItemView()
@property (nonatomic ,strong) QMUILabel *socLab;

@property (nonatomic ,strong) QMUILabel *powerLab;

@property (nonatomic ,strong) UIImageView *borderImg;

@property (nonatomic ,strong) ItemPorgressView *progressView;

@property (nonatomic ,strong) NSDictionary *dic;
@end
@implementation RHDeviceTopoBatteryItemView


-(instancetype)init
{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)setDataDic:(NSDictionary*)dic mainDeviceId:(NSString*)deviceId status:(BOOL)isOnline
{
    _dic = dic;
    NSString *socValue = [NSDecimalNumber decimalStringWithDouble:[dic[@"soc"]doubleValue] nullStr:@"--"];
    NSString *powerValue = [NSDecimalNumber decimalStringWithDoubleString:dic[@"power"] nullStr:@"--"];
    NSString *powerStr = [NSString stringWithFormat:@"%@ kW",isOnline?powerValue:@"--"];
    self.socLab.text =  [NSString stringWithFormat:@"%@%%",isOnline?socValue:@"--"];
    self.powerLab.attributedText = [NSString attributeStringWithContent:powerStr attriFont:kSYSTEMFONT(8) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:@"kW"];
    if ([dic[@"deviceId"] isEqualToString:deviceId]) {
        self.borderImg.image = [UIImage imageNamed:@"device_paralell_border"];
    }else{
        self.borderImg.image = [UIImage imageNamed:@"device_paralell_dashBorder"];
    }
    [self.progressView setProgress:[dic[@"soc"] floatValue]*0.01];
}

-(void)setupUI
{
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"5AD8A6");
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.centerX.mas_offset(0);
        make.top.mas_offset(0);
        make.height.offset(20);
    }];
    
    UIImageView *borderImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_paralell_dashBorder"]];
    [self addSubview:borderImage];
    self.borderImg = borderImage;
    [borderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(54, 54)));
        make.centerX.mas_offset(0);
        make.top.equalTo(line.mas_bottom);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    borderImage.userInteractionEnabled = YES;
    [borderImage addGestureRecognizer:tap];
    
    QMUILabel *socLab = [QMUILabel labelWithFont:kSYSTEMFONT(8) textColor:kCOLOR_HEX(@"5AD8A6") textAligment:NSTextAlignmentCenter];
    socLab.text = @"--%";
    [borderImage addSubview:socLab];
    self.socLab = socLab;
    [socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.mas_offset(5);
    }];
    
    UIImageView *batteryImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_paralell_batteryIcon"]];
    [borderImage addSubview:batteryImg];
    [batteryImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(19, 30)));
        make.centerX.mas_offset(0);
        make.bottom.mas_offset(-5);
    }];
    
    _progressView = [[ItemPorgressView alloc]init];
    _progressView.backgroundColor = UIColor.whiteColor;
    [borderImage addSubview:_progressView];
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(batteryImg).with.insets(UIEdgeInsetsMake(5, 3, 3, 3));
    }];
    [borderImage bringSubviewToFront:batteryImg];
//    QMUILabel *socTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentLeft];
//    socTitleLab.text = @"SOC";
//    [batView addSubview:socTitleLab];
//    [socTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(batteryImg).offset(5);
//        make.right.equalTo(batteryImg.mas_left).offset(- 5);
//        make.width.offset(40);
//    }];
    
    
    
    
    QMUILabel *batteryTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentLeft];
    batteryTitleLab.text = @"Battery";
    [self addSubview:batteryTitleLab];
    [batteryTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(borderImage.mas_bottom).offset(5);
        make.left.equalTo(borderImage);
    }];
    
    QMUILabel *batteryPowerLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentLeft];
    batteryPowerLab.text = @"--kW";
    [self addSubview:batteryPowerLab];
    self.powerLab = batteryPowerLab;
    [batteryPowerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(batteryTitleLab);
        make.top.equalTo(batteryTitleLab.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];

}

-(void)tapAction
{
    KWTTopTipView *tipView = [[KWTTopTipView alloc]init];
    self.borderImg.backgroundColor = kCOLOR_HEX_A(@"#5AD8A6", 0.2);
    [tipView showWithDeviceId:self.dic[@"deviceId"]];
    kWeakSelf;
    tipView.closeBlock = ^{
        weakSelf.borderImg.backgroundColor = UIColor.clearColor;
    };
}
@end
